package com.zuilapsa.jmc;

import org.apache.xmlrpc.*; 

import com.zuilapsa.jmc.userInterfaces.*;



public class Daemon extends Thread {

	private volatile Daemon Daemon;

	public Daemon(String name) {
		super(name);
		Daemon = this;
	}

	// public void run() {
	// while (Daemon != null) {
	// System.out.println(getName());
	// try {
	// sleep((long) (Math.random() * 1000));
	// } catch (InterruptedException e) {
	// }
	// }
	// }

	public void run() {
		Thread thisThread = Thread.currentThread();
		while (Daemon == thisThread) {
			System.out.println(getName());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				System.err.println(e);
			}
		}
	}

	public void startStopping() {
		Daemon = null;
	}

	public static void exportConfig() {
		try {
			Configuration conf = Configuration.getInstance();
			conf.exportManagedConfig(new SystemInterfaceSession());
		} catch (JmcException e) {
			System.err.println(e.getMessage());
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Daemon Daemon = new Daemon("Jelc");
//		Interpreter i = new Interpreter();
//		try {
//		i.eval("server(4080)");
//		} catch (EvalError e) {
//		}
		
		TelnetInterface telnet = new TelnetInterface();
		telnet.start();

		WebServer webserver = new WebServer(3081);
//		webserver.addHandler("examples", someHandler);
		webserver.setParanoid(true);
		webserver.acceptClient("127.0.0.1");

		if (args.length > 0 && args[0].equalsIgnoreCase("export")) {
			exportConfig();
		}
		// Daemon.start();

		// try {
		// Thread.sleep(10);
		// // Daemon.interrupt();
		// Thread.sleep(10);
		// Thread.currentThread().notify();
		// Thread.sleep(5000);
		// } catch (InterruptedException e){
		// }
		// Daemon.startStopping();
	}

}
